﻿namespace MuzoBlog.Core.Tools.MediaConverters
{
    using System;
    using System.Web;
    using MuzoBlog.Core.BussinessLogic.Medias;

    /// <summary>
    /// Ömer Faruk ZORLU
    /// 2010-12-08
    ///  </summary>
    public abstract class MediaConverter
    {
        private static string ffMpegPath = HttpContext.Current.Server.MapPath("~/files/exe/ffmpeg.exe");
        private static string flvToolPath = HttpContext.Current.Server.MapPath("~/files/exe/flvtool2.exe");

        public static bool Convert(string rawPath, string targetPath)
        {
            // Encoding process
            ConvertToFlv(rawPath, targetPath);

            if (System.IO.File.Exists(targetPath))
            {
                System.Threading.Thread.Sleep(250); // Wait for converters to release file
                
                EnsureBuffered(targetPath); // Fix buffer problem

                System.IO.File.Delete(rawPath);

                return true;
            }

            return false;
        }

        /// <summary>
        /// Converts FFMPeg supported media files to flv file.
        /// </summary>
        /// <param name="rawPath"></param>
        /// <param name="targetPath"></param>
        private static void ConvertToFlv(string rawPath, string targetPath)
        {
            System.Diagnostics.Process FFMpeg = new System.Diagnostics.Process();
            FFMpeg.StartInfo.FileName = ffMpegPath;
            FFMpeg.StartInfo.Arguments = "-y -i \"" + rawPath + "\" -f flv -ab 128 -ar 44100 -ac 1 -b 1000 -r 29.97 " +
                                            "-g 12 -qmin 3 -qmax 13 -sameq -s 320x240 \"" + targetPath + "\" ";
            FFMpeg.StartInfo.UseShellExecute = false;
            FFMpeg.StartInfo.CreateNoWindow = true;
            FFMpeg.StartInfo.RedirectStandardOutput = false;
            FFMpeg.Start();

            FFMpeg.WaitForExit();
        }

        /// <summary>
        /// Flv player buffer problem fixer. Use this method after flv convertion complated.
        /// </summary>
        /// <param name="targetPath"></param>
        public static void EnsureBuffered(string targetPath)
        {
            System.Diagnostics.Process FlvTool2 = new System.Diagnostics.Process();
            FlvTool2.StartInfo.FileName = flvToolPath;
            FlvTool2.StartInfo.Arguments = "-U " + targetPath;
            FlvTool2.StartInfo.UseShellExecute = false;
            FlvTool2.StartInfo.CreateNoWindow = true;
            FlvTool2.StartInfo.RedirectStandardOutput = false;
            FlvTool2.Start();

            FlvTool2.WaitForExit();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="videoPath">Video path</param>
        /// <param name="targetPath">Screen shot path</param>
        /// <param name="duration">00:00:10</param>
        public static void TakeScreenShoot(string videoPath, string targetPath, string duration)
        {
            System.Diagnostics.Process FFMpegResim = new System.Diagnostics.Process();
            FFMpegResim.StartInfo.FileName = ffMpegPath;
            FFMpegResim.StartInfo.Arguments = "-i " + videoPath + " -an -ss " + duration + " -r 1 -vframes 1 -s 320x240 -f mjpeg -y " + targetPath.Replace(".flv", "_k.jpg");
            FFMpegResim.StartInfo.UseShellExecute = false;
            FFMpegResim.StartInfo.CreateNoWindow = true;
            FFMpegResim.StartInfo.RedirectStandardOutput = false;
            FFMpegResim.Start();

            FFMpegResim.WaitForExit();
        }
    }
}